﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PhoneManager.LanguageText;

namespace PhoneManager
{
    public partial class FrmDefaultSettings : PhoneManager.FrmDefault
    {
        public FrmDefaultSettings()
        {
            InitializeComponent();
        }

        private void PnlFrm_Paint(object sender, PaintEventArgs e)
        {

        }

        private void BntClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmDefaultSettings_Load(object sender, EventArgs e)
        {
            LoadDefault();

            ChkAutoDetect.Checked = Properties.Settings.Default.DefaultAutoDetect;
            UpDnBattery.Value = Properties.Settings.Default.DefaultBatteryMinLevel;
            UpDnPBookPage.Value = Properties.Settings.Default.DefaultBookItemsOfPage;
            CboPBookCharSet.SelectedItem = Properties.Settings.Default.DefaultCharSet;
            TxtCodeCallOpe.Text = Properties.Settings.Default.DefaultCodeOper.Replace("X","");
            CboDTF.SelectedItem = Properties.Settings.Default.DefaultDateFmt;
            UpDnDelayCMD.Value = Properties.Settings.Default.DefaultDelayCommand;
            UpDnWPhone.Value = Properties.Settings.Default.DefaultLengthCelPhone;
            UpDnWDDD.Value = Properties.Settings.Default.DefaultLengthCodeDDD;
            UpDnOpenTM.Value = Properties.Settings.Default.DefaultOpenTimeOut / 1000;
            UpDnPollGoogle.Value = Properties.Settings.Default.DefaultPollingGoogle / 60;
            UpDnPollLive.Value = Properties.Settings.Default.DefaultPollingLive / 60;
            UpDnPollModem.Value = Properties.Settings.Default.DefaultPollingModem ;
            UpDnPollSMS.Value = Properties.Settings.Default.DefaultPollingSMS / 60;
            UpDnReadTM.Value = Properties.Settings.Default.DefaultReadTimeout / 1000;
            CboSMSCharSet.SelectedItem = Properties.Settings.Default.DefaultSMSCharSet;
            CboSMSReadDelete.SelectedItem = ResourcesText.WordSIM;
            if (Properties.Settings.Default.DefaultSMSReadDelete == "ME")
            {
                CboSMSReadDelete.SelectedItem = ResourcesText.WordPhone;
            }
            CboSMSReceiver.SelectedItem = ResourcesText.WordSIM;
            if (Properties.Settings.Default.DefaultSMSReceiver == "ME")
            {
                CboSMSReceiver.SelectedItem = ResourcesText.WordPhone;
            }
            CboSMSWriteSend.SelectedItem = ResourcesText.WordSIM;
            if (Properties.Settings.Default.DefaultSMSWriteSend == "ME")
            {
                CboSMSWriteSend.SelectedItem = ResourcesText.WordPhone;
            }
            
            UpDnSyncPage.Value = Properties.Settings.Default.DefaultSyncItemsOfPage;
            UpDnWriteTM.Value = Properties.Settings.Default.DefaultWriteTimeout / 1000;

            this.Text = ResourcesText.DefaultFor + " " + SharedLib.ModemGSM.Model;
        }
        private void LoadDefault()
        {
            DefaultSeetings defset = new DefaultSeetings();
            ChkAutoDetect.Checked = defset.AutoDetect;
            UpDnBattery.Value = defset.BatteryLevel;
            UpDnPBookPage.Value = defset.PageItemsPhonebook;
            CboPBookCharSet.SelectedItem = defset.CharSetPhonebook;
            TxtCodeCallOpe.Text = defset.CallCodeOper.Replace("X","");
            CboDTF.SelectedItem = defset.DateFormat;
            UpDnDelayCMD.Value = defset.DelayCmd;
            UpDnWPhone.Value = defset.StdLenPhone;
            UpDnWDDD.Value = defset.StdLenDDD;
            UpDnOpenTM.Value = defset.OpenTimeOut / 1000;
            UpDnPollGoogle.Value = defset.PoollingGoogle / 60;
            UpDnPollLive.Value = defset.PoollingLive / 60;
            UpDnPollModem.Value = defset.PoollingModem;
            UpDnPollSMS.Value = defset.PoollingSMS / 60;
            UpDnReadTM.Value = defset.ReadTimeOut / 1000;
            CboSMSCharSet.SelectedItem = defset.CharSetSMS;
            CboSMSReadDelete.Items.Clear();
            CboSMSReadDelete.Items.Add(ResourcesText.WordSIM);
            CboSMSReadDelete.Items.Add(ResourcesText.WordPhone);
            CboSMSReadDelete.SelectedItem = ResourcesText.WordSIM;
            if (defset.SMSReadDelete == "ME")
            {
                CboSMSReadDelete.SelectedItem = ResourcesText.WordPhone;
            }
            CboSMSReceiver.Items.Clear();
            CboSMSReceiver.Items.Add(ResourcesText.WordSIM);
            CboSMSReceiver.Items.Add(ResourcesText.WordPhone);
            CboSMSReceiver.SelectedItem = ResourcesText.WordSIM;
            if (defset.SMSReceiver == "ME")
            {
                CboSMSReceiver.SelectedItem = ResourcesText.WordPhone;
            }
            CboSMSWriteSend.Items.Clear();
            CboSMSWriteSend.Items.Add(ResourcesText.WordSIM);
            CboSMSWriteSend.Items.Add(ResourcesText.WordPhone);
            CboSMSWriteSend.SelectedItem = ResourcesText.WordSIM;
            if (defset.SMSWriteSend == "ME")
            {
                CboSMSWriteSend.SelectedItem = ResourcesText.WordPhone;
            }
            UpDnSyncPage.Value = defset.PageItemsSync ;
            UpDnWriteTM.Value = defset.WriteTimeOut / 1000;
        }

        private void BntLoad_Click(object sender, EventArgs e)
        {
            LoadDefault();
        }

        private void BntSave_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.DefaultAutoDetect = ChkAutoDetect.Checked;
            Properties.Settings.Default.DefaultBatteryMinLevel = (int)UpDnBattery.Value;
            Properties.Settings.Default.DefaultBookItemsOfPage = (int)UpDnPBookPage.Value;
            Properties.Settings.Default.DefaultCharSet = CboPBookCharSet.Text;
            Properties.Settings.Default.DefaultCodeOper = TxtCodeCallOpe.Text + "".PadLeft((int)UpDnWDDD.Value,'X');
            Properties.Settings.Default.DefaultDateFmt = CboDTF.Text;
            Properties.Settings.Default.DefaultDelayCommand = (int)UpDnDelayCMD.Value;
            Properties.Settings.Default.DefaultLengthCelPhone = (int)UpDnWPhone.Value;
            Properties.Settings.Default.DefaultLengthCodeDDD = (int)UpDnWDDD.Value;
            Properties.Settings.Default.DefaultOpenTimeOut = (int)UpDnOpenTM.Value * 1000;
            Properties.Settings.Default.DefaultPollingGoogle = (int)UpDnPollGoogle.Value * 60;
            Properties.Settings.Default.DefaultPollingLive = (int)UpDnPollLive.Value * 60;
            Properties.Settings.Default.DefaultPollingModem = (int)UpDnPollModem.Value;
            Properties.Settings.Default.DefaultPollingSMS = (int)UpDnPollSMS.Value * 60;
            Properties.Settings.Default.DefaultReadTimeout = (int)UpDnReadTM.Value * 1000;
            Properties.Settings.Default.DefaultSMSCharSet = CboSMSCharSet.Text ;
            Properties.Settings.Default.DefaultSMSReadDelete = "SM";
            if (CboSMSReadDelete.Text == ResourcesText.WordPhone)
            {
                Properties.Settings.Default.DefaultSMSReadDelete = "ME";
            }
            Properties.Settings.Default.DefaultSMSReceiver = "SM";
            if (CboSMSReceiver.Text == ResourcesText.WordPhone)
            {
                Properties.Settings.Default.DefaultSMSReceiver = "ME";
            }
            Properties.Settings.Default.DefaultSMSWriteSend = "SM";
            if (CboSMSWriteSend.Text == ResourcesText.WordPhone)
            {
                Properties.Settings.Default.DefaultSMSWriteSend = "ME";
            }
            Properties.Settings.Default.DefaultSyncItemsOfPage = (int)UpDnSyncPage.Value;
            Properties.Settings.Default.DefaultWriteTimeout = (int)UpDnWriteTM.Value * 1000;
            SharedLib.SaveSettings();

            SharedLib.ChangeDefaultSet = true;
            this.Close();
        }

        private void UpDnWDDD_ValueChanged(object sender, EventArgs e)
        {
            LblOpeComp.Text = "+ " + "".PadLeft((int)UpDnWDDD.Value, 'X');
        }

        private void TxtCodeCallOpe_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')
            {
                e.Handled = false;
                return;
            }
            else if ((e.KeyChar < '0') || (e.KeyChar > '9'))
            {
                e.Handled = true;
            }
        }
    }
}
